package com.cf.cachedcrud.service;

/**
 * cf-game-BaseService
 *
 * @author <a href="2428922347@qq.com">Zhu mingming</a>
 * @info
 * @since 2018/3/1 18:14
 */


import com.cf.cache.ZCache;
import com.cf.cache.core.CacheUpdatePusher;
import com.cf.cachedcrud.CacheKeyUtil;
import com.cf.cachedcrud.entity.CacheEntity;
import com.cf.crud.mapper.BaseMapper;
import com.cf.crud.service.AbsBaseService;
import com.cf.crud.service.BaseService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

/**
 * wx-hongbao-BaseService
 *
 * @author <a href="2428922347@qq.com">Zhu mingming</a>
 * @info
 * @since 2018/1/15 15:02
 */
public abstract class AbsCacheBaseService<T extends CacheEntity, M extends BaseMapper<T>> extends AbsBaseService<T, M> {

    @Autowired
    private CacheUpdatePusher cacheUpdatePusher;

    public T insert(T bean) {
        int insert = mapper.insert(bean);
        return insert > 0 ? bean : null;
    }

    public boolean inserts(List<T> beans) {
        int insert = mapper.insertListWithId(beans);
        return insert > 0 ? true : false;
    }

    @Override
    public int delete(T bean) {
        cacheUpdatePusher.push(bean.getCacheKey());
        return mapper.delete(bean);
    }

    @Override
    public Integer deleteById(long id) {
        cacheUpdatePusher.push(CacheKeyUtil.getCacheKey(this.getTargetClass(), id));
        return this.mapper.deleteByPrimaryKey(id);
    }

    @Override
    public int update(T bean) {
        cacheUpdatePusher.push(CacheKeyUtil.getCacheKey(this.getTargetClass(), bean.getId()));
        return mapper.updateByPrimaryKey(bean);
    }

}
